<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>搜索下拉框</title>
    <style>
        /* 基础样式 */
        .search-container {
            position: relative;
            width: 300px;
            margin: 50px auto;
            font-family: Arial, sans-serif;
        }

        /* 输入框样式 */
        .search-input {
            width: 100%;
            padding: 12px 20px;
            font-size: 16px;
            border: 2px solid #ddd;
            border-radius: 25px;
            outline: none;
            transition: border-color 0.3s ease;
            box-sizing: border-box;
        }

        /* .search-input:focus {
            border-color: #4CAF50;
        } */

        /* 下拉框样式 */
        .dropdown {
            position: absolute;
            width: 100%;
            max-height: 300px;
            margin-top: 5px;
            background: white;
            border: 1px solid #ddd;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            overflow-y: auto;
            display: none; /* 默认隐藏 */
        }

        /* 显示下拉框 */
        /* .search-input:focus + .dropdown {
            display: block;
        } */

        /* 下拉选项样式 */
        .dropdown-item {
            padding: 12px 20px;
            cursor: pointer;
            transition: background 0.2s ease;
            color: #333;
        }

        .dropdown-item:hover {
            background: #f5f5f5;
        }
        .focusColor {
          border-color: #4CAF50;
        }
    </style>
</head>
<body>
    <div class="search-container">
        <input 
            type="search" 
            class="search-input" 
            placeholder="请输入搜索内容..."
            autocomplete="off"
        >
        <div class="dropdown">
            <div class="dropdown-item">示例结果 1</div>
            <div class="dropdown-item">示例结果 2</div>
            <div class="dropdown-item">示例结果 3</div>
            <div class="dropdown-item">示例结果 4</div>
        </div>
    </div>
    <script>
      // 属性选择器
      const input = document.querySelector("input[type=search]");
      input.addEventListener("focus", () => {
        const dropdown = document.querySelector(".dropdown");
        dropdown.style.display = "block";
        input.classList.add("focusColor");
      });
      input.addEventListener("blur", () => {
        const dropdown = document.querySelector(".dropdown");
        dropdown.style.display = "none";
        input.classList.remove("focusColor");
      });
    </script>
</body>
</html>
